<?php
namespace App\Http\Controllers;


use App\Http\Controllers;
use App\Product;
use App\Rad_users;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\UsersController as users;
use App\Http\Controllers\AliPay\AliPayRequestController as AliPay;
use App\Http\Controllers\Wxpay\WxPayController as Wxpay;
use App\Order;
use App\User;

class PayController extends Controller
{
    var $time = null;

    public function __construct()
    {
        $this->time = date('Y-m-d H:i:s',time());
        header("Content-type:text/html;charset=utf-8");
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 调起阿里支付
     */
    public function SlingAliPay(Request $request)
    {
        $user_id = '';
        $to_user = '';
        $coupon_code = '';
        //  用户token
        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => '用户信息有误，请重新登录',
                'data' => array()
            );
            return response()->json($return);
        }

        $token = $request->input('token');

        //   订单号
        if (empty($request->input('order_no'))){
            $return = array(
                'code' => -1,
                'msg' => '订单号有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $order_no = $request->input('order_no');


        if (empty($request->input('price'))){
            $return = array(
                'code' => -1,
                'msg' => '价格有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $price = $request->input('price');


        //   产品数量
        if (empty($request->input('month'))){
            $return = array(
                'code' => -1,
                'msg' => '产品数量有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $month = $request->input('month');


        if (empty($request->input('pay_id'))){
            $return = array(
                'code' => -1,
                'msg' => '价格有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $pay_id = $request->input('pay_id');

        $product = Product::find($pay_id);

        //   会员类型
        if (empty($request->input('vip'))){
            $return = array(
                'code' => -1,
                'msg' => '会员类型有误',
                'data' => array()
            );
            return response()->json($return);
        }

        $vip = $request->input('vip');

        //   获取用户的ID
        $user_id = users::getIdByWebToken($token);


        //  给谁充值
        if($request->input('to_user'))
        {
            $to_user = $request->input('to_user');
        }else{
            $to_user = $user_id;
        }


        //限制为多连接用户购买套餐

        $user = Rad_users::find($to_user);
        if($user == NULL)
        {
            $return = array(
                'code' => -1,
                'msg' => "非法的参数！",
                'data' => array()
            );
            return response()->json($return);
        }


        if($user->con_max>1){
            $return = array(
                'code' => -1,
                'msg' => "多连接用户请联系客服购买套餐！",
                'data' => array()
            );
            return response()->json($return);
        }

        //   代理ID
        if($request->input('agent_id'))
        {
            $agent_id = $request->input('agent_id');
        }else{
            $agent_id = 1;              //   牛魔
        }


        //  优惠号码
        if($request->input('coupon_code'))
        {
            $coupon_code = $request->input('coupon_code');
        }


        //   原价
        if (empty($request->input('price_origin'))){
            $return = array(
                'code' => -1,
                'msg' => '原价有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $price_origin = $request->input('price_origin');

        $pay_terminal = 'pc';
        if($request->has('pay_terminal'))
        {
            $pay_terminal = $request->input('pay_terminal');
        }


        //  接口添加
        $order = new Order();
        $order->month = $month;                             //   购买产品数量
        $order->product_id = $pay_id;                       //   产品ID
        $order->order_no = $order_no;                       //  订单号
        $order->user_id = $user_id;                         //   用户ID
        $order->price = $price;                             //   现价
        $order->date = $product->date;                            //   购买产品天数
        $order->to_user = $to_user;                         //   给充值用户    没有给自己
        $order->pay_channel = 1;                            //  支付宝  1  微信  2
        $order->agent_id = $agent_id;                       //  代理ID   默认牛魔
        $order->coupon_code = $coupon_code;                 //  优惠码
        $order->vip = $vip;                                 //  会员类型标识
        $order->created_at = $this->time;                   //  创建时间
        $order->updated_at = $this->time;                   //  更新时间
        $order->connect_num = 1;                            //  连接数
        $order->account_type = '付费';                      //  账户类型
        $order->terminal_type = 1;                          //  pc端  1
        $order->save();
        $data = [];

        $alipay = new AliPay();
        $subject = "动态IP套餐";
        $body="动态IP套餐。";
        $extra_common_param = 'pay';
        $data =  $alipay->SendRequest($order_no,$price,$pay_terminal,$body,$subject,$extra_common_param);

        $return = array(
            'code' => 0,
            'msg' => '',
            'data' => $data
        );
        return response()->json($return);

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     *
     * 吊起微信支付
     */
    public function SlingWxPay(Request $request)
    {
        $user_id = '';
        $to_user = '';
        $coupon_code = '';
        //  用户token
        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => '用户信息有误，请重新登录',
                'data' => array()
            );
            return response()->json($return);
        }

        $token = $request->input('token');

        //   订单号
        if (empty($request->input('order_no'))){
            $return = array(
                'code' => -1,
                'msg' => '订单号有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $order_no = $request->input('order_no');


        if (empty($request->input('price'))){
            $return = array(
                'code' => -1,
                'msg' => '价格有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $price = $request->input('price');


        //   产品数量
        if (empty($request->input('month'))){
            $return = array(
                'code' => -1,
                'msg' => '产品数量有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $month = $request->input('month');


        if (empty($request->input('pay_id'))){
            $return = array(
                'code' => -1,
                'msg' => '价格有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $pay_id = $request->input('pay_id');

        $product = Product::find($pay_id);

        //   会员类型
        if (empty($request->input('vip'))){
            $return = array(
                'code' => -1,
                'msg' => '会员类型有误',
                'data' => array()
            );
            return response()->json($return);
        }

        $vip = $request->input('vip');

        //   获取用户的ID
        $user_id = users::getIdByWebToken($token);


        //  给谁充值
        if($request->input('to_user'))
        {
            $to_user = $request->input('to_user');
        }else{
            $to_user = $user_id;
        }


        //限制为多连接用户购买套餐

        $user = Rad_users::find($to_user);
        if($user == NULL)
        {
            $return = array(
                'code' => -1,
                'msg' => "非法的参数！",
                'data' => array()
            );
            return response()->json($return);
        }


        if($user->con_max>1){
            $return = array(
                'code' => -1,
                'msg' => "多连接用户请联系客服购买套餐！",
                'data' => array()
            );
            return response()->json($return);
        }

        //   代理ID
        if($request->input('agent_id'))
        {
            $agent_id = $request->input('agent_id');
        }else{
            $agent_id = 1;              //   牛魔
        }


        //  优惠号码
        if($request->input('coupon_code'))
        {
            $coupon_code = $request->input('coupon_code');
        }


        //   原价
        if (empty($request->input('price_origin'))){
            $return = array(
                'code' => -1,
                'msg' => '原价有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $price_origin = $request->input('price_origin');

        $pay_terminal = 'pc';
        if($request->has('pay_terminal'))
        {
            $pay_terminal = $request->input('pay_terminal');
        }



        //  接口添加
        $order = new Order();
        $order->month = $month;                             //   购买产品数量
        $order->product_id = $pay_id;                       //   产品ID
        $order->order_no = $order_no;                       //  订单号
        $order->user_id = $user_id;                         //   用户ID
        $order->price = $price;                             //   现价
        $order->date = $product->date;                      //   购买产品天数
        $order->to_user = $to_user;                         //   给充值用户    没有给自己
        $order->pay_channel = 2;                            //  支付宝  1  微信  2
        $order->agent_id = $agent_id;                       //  代理ID   默认牛魔
        $order->coupon_code = $coupon_code;                 //  优惠码
        $order->vip = $vip;                                 //  会员类型标识
        $order->created_at = $this->time;                   //  创建时间
        $order->updated_at = $this->time;                   //  更新时间
        $order->connect_num = 1;                            //  连接数账户类型
        $order->account_type = '付费';                      //
        $order->terminal_type = 1;                          //  pc端  1
        $order->save();

        $wxpay = new Wxpay();
        $data = $wxpay->getWxpayCode($body='牛魔科技-动态IP套餐-',$attach='pay',$out_trade_no=$order_no,$total_fee=$price,$Goods_tag=$product->name,$product_id=$pay_id);


        $return = array(
            'code' => 0,
            'msg' => '',
            'data' => $data
        );
        return response()->json($return);

    }





}
